/**
 * ArtworkDatabaseReader.java
 * Copyright (C) 2013 Konstantin Grigoryev
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package aspid.artwork;

import java.io.*;
import java.nio.charset.*;
import javax.imageio.stream.*;
import java.nio.ByteOrder;

import aspid.artwork.chunk.*;
import aspid.artwork.chunk.factory.*;


public class ArtworkDatabaseReader
{
    ChunkFactory factory = new ChunkFactory()
    {{
        addPrototype(new ItemChunk("mhfd"));
        addPrototype(new ItemChunk("mhsd"));
        addPrototype(new ListChunk("mhli"));
        addPrototype(new ItemChunk("mhii"));
        addPrototype(new ItemChunk("mhod"));
        addPrototype(new ItemChunk("mhni"));
        addPrototype(new ItemChunk("mhaf"));
        addPrototype(new ListChunk("mhla"));
        addPrototype(new ListChunk("mhlf"));
        addPrototype(new ItemChunk("mhif"));
    }};

    ImageInputStream stream;
    ChunkReader      reader;

    public ArtworkDatabaseReader( ImageInputStream stream )
    {
        this.stream = stream;
        stream.setByteOrder(ByteOrder.LITTLE_ENDIAN);

        this.reader = new ChunkReader(stream);
    }

    private Chunk readChunk()
        throws IOException, ChunkNotFoundException
    {
        byte[] magic = new byte[4];
        stream.readFully(magic);
        String name = new String(magic, StandardCharsets.US_ASCII);
        if (!name.matches("mh.."))
            return null;

        Chunk chunk = factory.createChunk(name);
        reader.readChunk(chunk);
        return chunk;
    }

    private String readCharacterData()
        throws IOException
    {
        int numBytes = stream.readInt();
        byte[] bytes = new byte[numBytes];
        stream.skipBytes(8);
        stream.readFully(bytes);
        return new String(bytes, StandardCharsets.UTF_16LE);
    }

    public void readDatabase()
        throws IOException, ChunkNotFoundException
    {
        while (stream.getStreamPosition() != stream.length()) {
            stream.mark();
            Chunk chunk = readChunk();
            if (chunk != null) {
                System.out.println(chunk);
                continue;
            }

            stream.reset();
            String string = readCharacterData();
            System.out.println(string);
        }
    }

    public static void main( String[] args )
        throws IOException
    {
        File file = new File("data/ArtworkDB");
        try (ImageInputStream stream = new FileImageInputStream(file)) {
            ArtworkDatabaseReader reader = new ArtworkDatabaseReader(stream);
            reader.readDatabase();
        }
        catch (ChunkNotFoundException ex) {
            System.err.println("Unexpected chunk name: " + ex.getMessage());
        }
    }
}
